const state = {
    roles: {}
}
const actions = {
    saveRole: ({ commit }, payload) => {
        commit('SAVEROLE', payload)
    },
    updateRole: ({ commit }, payload) => {
        commit('UPDATEROLE', payload)
    },
    add_role: ({ commit }, payload) => {
        commit('ADDROLE', payload)
    },
    del_role: ({ commit }, payload) => {
        commit('DELROLE', payload)
    }
}
const mutations = {
    SAVEROLE: (state, payload) => state.roles = payload.result,
    UPDATEROLE: (state, payload) => {
        let roles = state.roles.content
        let index = roles.findIndex(it => it.id === payload.result.id)
        roles.splice(index, 1, payload.result)
    },
    ADDROLE: (state, payload) => {
        state.roles.content.push(payload.result)
    },
    DELROLE: (state, payload) => {
        let roles = state.roles.content
        let index = roles.findIndex(it => it.id === payload)
        roles.splice(index, 1)
    }
}
const getters = {
    getRoles: state => state.roles
}

export default {
    state,
    mutations,
    actions,
    getters
}